#
# Popup.py
#
# Author: Elysia Haight
#

from Tkinter import *
from os.path import *
from Game import *

# This class creates a different popup depending on the move
class Popup(Toplevel) :

    #possible win sates
    NO_WIN = 0
    BREAKER_WIN = 1
    MAKER_WIN = 2

    # The constructor creates the basis of the popup
    def __init__(self) :
        self.popup = Toplevel()
        self.message = Message(self.popup)
        self.button = Button(self.popup, text="OK", command=self.popup.quit)
        
    # Pop up informing the user of a new game
    def new_game_popup(self) :
        self.message.config(text="Player has selected a new game.")
        self.message.pack(side=TOP)

        self.button.pack()

    # Pop up informing the user of a player exit
    def exit_game_popup(self) :
        self.message.config(text="Player has exited the game.")
        self.message.pack(side=TOP)
        self.button.pack()

    # Pop up informing the user of an undo
    def undo_popup(self) :
        self.message.config(text="Player has selected 'undo'")
        self.message.pack(side=TOP)
        self.button.pack(side=BOTTOM)

    # Pop up informing the user of a winner
    def win_game_popup(self, winner) :
        if winner == self.BREAKER_WIN :
            self.message.config(text="Breaker has won the game.")
        elif winner == self.MAKER_WIN :
            self.message.config(text="Maker has won the game.")

        self.message.pack(side=TOP)
        self.button.pack(side=BOTTOM)

    # Pop up asking for a log path input value
    def log_path_popup(self) :
        self.message.config(text="Please enter a file name for log.")
        self.input = Entry(self.popup)
        self.cancel_button = Button(self.popup, text="Cancel", command=self.
                                    popup.quit)
        self.button.config(command=self.file_check())

        self.message.pack(side=TOP)
        self.input.pack(side=TOP)
        self.button.pack(side=LEFT)
        self.cancel_button.pack(side=LEFT)

    # Pop up asking for confirmation of file overwrite
    def overwrite_popup(self) :
        self.message.config(text="Would you like to overwrite this file?")
        self.cancel_button.config(command=self.popup.quit)

        self.message.pack(side=TOP)
        self.button.pack(side=LEFT)
        self.cancel_button.pack(side=LEFT)

    #Ensure file name has been entered
    #If file already exists, confirm name with user
    def file_check(self) :

        self.filename = self.input.get()

        #Ensure file name has been entered; pop up error message if one hasn't
        if len(self.filename) == 0 :
            self.error_message = Message(self.popup, text="Enter a file name",
                                         fg = 'red')
            self.error_message.pack(side=BOTTOM)
        else :
            if os.path.exists(self.filename) == True :
                self.overwrite_popup(self)
            else :
                self.Log = Logging(self.game, self.filename)
